<?php

if(!empty($_GET['id'])){
	$id=$_GET['id'];
};


if(!empty($_POST)){
	$id=$_GET['id'];
	$img='';
		if(!empty($_POST['delimg'])){
			foreach ($_POST['delimg'] as $key => $value) { //删除旧图片
				unlink($value);
			}
		unset($_POST['delimg']);
		}
		if(!empty($_POST['oldimg'])){
			foreach ($_POST['oldimg'] as $k => $val) {//获取没修改的图片
				$img.=$val.',';
			}
			unset($_POST['oldimg']);
		}
	$img=substr($img,0,-1);//截取最后的字符串
	date_default_timezone_set('ASIA/Shanghai');
	$id=$_GET['id'];
	$time=date('Y-m-d H:i:s',time());			//时间
	$_POST['time']=$time;
	$data=$_POST;
if(!empty($_FILES['image']['name'][0])){
	$arr=array();
	foreach ($_FILES['image'] as $key => $value) {   //重组数组
		foreach ($value as $keys => $val) {
			$arr[$keys][$key]=$val;
		}
	}
	$imagetype=array('.jpg','.jpeg','.bmp','.gif','.png','.psd');
		$y=date('Y');
		$m=date('m');
		$d=date('d');
		$path='upload/'.$y.'/'.$m.'/'.$d;//路径
		if(!file_exists($path)){//判断文件是否存在
			mkdir($path,777,true);//创建多级别文件夹
		};
		foreach ($arr as $k => $v) {
		$type=substr($v['name'], strripos($v['name'], '.'));//后缀
		// echo $type;print_r($imagetype);exit;
		if(!in_array($type,$imagetype)){echo '<script>alert ("意外的文件类型！");location="index.php?admin=1&c=goods&a=list";</script>';exit;}; //判断后缀
		if($v['size']>5*1024*1024){echo '<script>alert ("文件超过限制大小！");location="index.php?admin=1&c=goods&a=list";</script>';exit;};
		$imgname=$path.'/'.substr(md5(date('YmdHis')),15).rand(1000,9999);//拼上文件名
		$imgname.=$type;//拼上后缀
		
		//                   文件当前路径               更改的路径
		move_uploaded_file($v['tmp_name'],$imgname);//移动文件
		thumb($imgname,$type);//调用缩略图函数
		water($imgname);//调用水印函数
			if(!empty($img)){
				$img.=','.$imgname;
			}else{
				$img=$imgname;
			}
		
		}
		$data['image']=$img;//传回post

	};
	$row=update('goods',$data,'id='.$id);
	if($row==1){
		echo '<script>alert ("修改成功");location="index.php?admin=1&c=goods&a=list";</script>';
		};
	
}

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])){
	$id=empty($_POST['id'])?0:$_POST['id'];
	$arr=getlist('category','pid='.$id);
	echo json_encode($arr);exit;
}

$arr=getone('goods',"id=$id");
$explode=explode(',',$arr['image']);
$arr['image']=$explode;

$catearr=$arr;//当前记录赋值给中间数组

$catearr['pid']=$catearr['category_id'];
$cate=array();

do{
	$pid=$catearr['pid'];             //获取当前选项的pid
	$arr1=getlist('category','pid='.$pid);//获取当前选项同级列表
	$cate[$catearr['id']]=$arr1;						  //丢进去
	$catearr=getone('category','id='.$pid);  //获取当前的上一级列表
               
}while($pid!=0);
unset($cate[$id]);
$cate=array_reverse($cate,true);//反转数组
// print_r($cate);exit;
$brand=getlist('brand');
$data=array('arr'=>$arr,'cate'=>$cate,'brand'=>$brand);
view($data);
?>